package com.daykw.xiaohongshu.task.thread;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.impl.NutDao;

import com.alibaba.fastjson.JSONObject;
import com.daykw.xiaohongshu.http.HttpBase;
import com.daykw.xiaohongshu.model.TArticleInfo;
import com.daykw.xiaohongshu.model.TArticleTask;
import com.daykw.xiaohongshu.model.TAuthorTask;
import com.daykw.xiaohongshu.model.UpdateAid;
import com.daykw.xiaohongshu.mq.TQueue;
import com.daykw.xiaohongshu.util.Const;
import com.daykw.xiaohongshu.util.IPPoolMap;
import com.daykw.xiaohongshu.util.MongoDBUtil;
import com.daykw.xiaohongshu.util.SpringContextHolder;
import com.mongodb.client.MongoCollection;

public class RestArticleThread extends Thread {
	private static final Logger logger = Logger.getLogger(RestArticleThread.class);

	private UpdateAid info;

	public RestArticleThread(UpdateAid info) {
		this.info = info;
	}

	@Override
	public synchronized void run() {
		consume();

	}

	public void consume() {
		NutDao xhsDao = SpringContextHolder.getBean("dao");
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat fdate = new SimpleDateFormat("yyyyMMddHH");
		SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd");
		logger.info("****重置文章线程启动消息*****");

		try {

			xhsDao.update(UpdateAid.class, Chain.make("status", 1), Cnd.where("aid", "=", info.getAid()));

			int nowdate = Integer.parseInt(fdate.format(new Date()));

			// String url = info.getUrl();
			String newurl = "http://www.xiaohongshu.com/api/sns/v9/note/" + info.getAid()
					+ "?deviceId=B5E5ABE1-8868-42FA-9630-CAE01E6139CD&device_fingerprint=201806091127168d847071c3d718390bb917a3fedfae240138edc103f8f009&device_fingerprint1=201806091127168d847071c3d718390bb917a3fedfae240138edc103f8f009&lang=zh&oid=5b24935f9374260196ec59cf&platform=iOS&sid=session.1214697449282265726&sign=347818209541e474a2f8f1090f43ee85&source=explore&t=1530201458";
			String article_id = info.getAid();
			try {
				Map<String, String> headers = new HashMap<String, String>();

				headers.put("Host", "www.xiaohongshu.com");
				headers.put("Accept-Encoding", "br, gzip, deflate");
				headers.put("Connection", "keep-alive");
				headers.put("device_id", "B5E5ABE1-8868-42FA-9630-CAE01E6139CD");
				headers.put("Accept", "application/json");
				headers.put("User-Agent",
						"discover/5.17.1 (iPhone; iOS 12.0; Scale/2.00) Resolution/640*1136 Version/5.17.1 Build/5171001 Device/(Apple Inc.;iPhone8,4)");
				headers.put("Accept-Language", "zh-Hans-CN;q=1");
				headers.put("Host", "www.xiaohongshu.com");

				headers.put("Authorization", "session.1213010864761185755");
				headers.put("X-Tingyun-Id", "LbxHzUNcfig;c=2;r=1813010760");
				// webdriver指针指定获取哪一个webdriver

				String html = new HttpBase().get(newurl, "utf-8", headers, true);
				while (StringUtils.isNotEmpty(html) && html.equals("fail")) {

					html = new HttpBase().get(newurl, "utf-8", headers, true);

					if (html.equals("404")) {
						break;
					}

				}

				if (html != null && html.equals("404")) {
					xhsDao.update(UpdateAid.class, Chain.make("status", 5), Cnd.where("id", "=", info.getId()));
					logger.info("**********404:" + info.getAid() + "************");
					return;
				}

				JSONObject all = JSONObject.parseObject(html);

				if (all != null && !all.isEmpty() && all.getBoolean("success")) {
					JSONObject obj = all.getJSONObject("data");
					if (obj == null || obj.isEmpty()) {
						xhsDao.update(UpdateAid.class, Chain.make("status", 2), Cnd.where("id", "=", info.getId()));
						return;
					}

					String pub_date = null;
					String like_num = null;
					String comment_num = null;
					String collect_num = null;
					String aid = null;

					JSONObject author = obj.getJSONObject("user");

					String uid = author.getString("userid");
					String nickname = author.getString("nickname");
					Long long_date = obj.getLong("post_time");

					long_date = long_date * 1000L;

					pub_date = formatter.format(new Date(long_date));

					aid = obj.getString("id");

					like_num = obj.getString("likes");
					comment_num = obj.getString("comments");
					collect_num = obj.getString("fav_count");
					if (!article_id.equals(aid)) {
						xhsDao.update(UpdateAid.class, Chain.make("status", 0), Cnd.where("id", "=", info.getId()));
						logger.info("######文章id不一致," + article_id + "：" + aid + "#######");
						// FileWrite.wirteFile("D:\\xhs.html", doc.html());

						return;
					}

					TAuthorTask u = xhsDao.fetch(TAuthorTask.class, Cnd.where("user_id", "=", uid));
					if (u == null) {

						u = new TAuthorTask();
						u.setCreate_time(new Date());
						u.setStatus(0);
						u.setUser_id(uid);
						u.setNick(nickname);
						u.setUrl("http://www.xiaohongshu.com/user/profile/" + uid);
						xhsDao.fastInsert(u);

					} else {
						xhsDao.update(UpdateAid.class, Chain.make("user_id", uid), Cnd.where("id", "=", info.getId()));
					}

					TArticleTask ta = xhsDao.fetch(TArticleTask.class, Cnd.where("aid", "=", info.getAid()));
					if(ta==null) {
						ta = new TArticleTask();
						ta.setAid(aid);
						ta.setComment_status(0);
						ta.setCreate_time(new Date());
						ta.setStatus(0);
						ta.setUser_id(uid);
						ta.setUrl("http://www.xiaohongshu.com/discovery/item/" +aid);
						xhsDao.fastInsert(ta);
					}else {
						if (!ta.getUser_id().equals(uid)) {
							xhsDao.update(UpdateAid.class, Chain.make("status", 9), Cnd.where("id", "=", info.getId()));
							return;
						}
					}
					
					Integer like = 0;
					Integer comment = 0;
					Integer collect = 0;
					if (StringUtils.isNotEmpty(like_num)) {
						try {
							like = Integer.parseInt(like_num);
						} catch (Exception e1) {
							logger.error("", e1);
						}
					}

					if (StringUtils.isNotEmpty(comment_num)) {
						try {
							comment = Integer.parseInt(comment_num);
						} catch (Exception e1) {
							logger.error("", e1);
						}
					}

					if (StringUtils.isNotEmpty(collect_num)) {
						try {
							collect = Integer.parseInt(collect_num);
						} catch (Exception e1) {
							logger.error("", e1);
						}
					}

					TArticleInfo r = new TArticleInfo();
					try {

						r.setArticle_id(article_id);
						r.setLikes(like);
						r.setCollects(collect);
						r.setComments(comment);
						try {
							Date pdate = formatter.parse(pub_date);
							r.setPub_date(pdate);
							r.setPdate(Integer.parseInt(date.format(pdate)));
						} catch (Exception de) {
							try {
								Date pdate = fm.parse(pub_date);
								r.setPub_date(pdate);
								r.setPdate(Integer.parseInt(date.format(pdate)));
							} catch (Exception e) {
								xhsDao.update(UpdateAid.class, Chain.make("status", 3),
										Cnd.where("id", "=", info.getId()));
								return;
							}

						}
						r.setUser_id(uid);
						r.setCreate_date(nowdate);

						r.setUrl(info.getUrl());

						// r.setJson(row.toJSONString());
						xhsDao.fastInsert(r);

					} catch (Exception e1) {

						xhsDao.update(TArticleInfo.class,
								Chain.make("collects", collect).add("comments", comment).add("likes", like),
								Cnd.where("article_id", "=", article_id));

					}
					MongoCollection<org.bson.Document> coll = MongoDBUtil.instance.getCollection(Const.MDB_DB,
							"article");
					try {

						org.bson.Document mjson = org.bson.Document.parse(obj.toJSONString());

						mjson.put("aid", article_id);
						mjson.put("user_id", r.getUser_id());
						mjson.put("pdate", r.getPdate());
						mjson.put("cdate", nowdate);
						mjson.put("isnew", 1);
						coll.insertOne(mjson);
						xhsDao.update(UpdateAid.class, Chain.make("status", 2), Cnd.where("id", "=", info.getId()));

					} catch (com.mongodb.MongoWriteException e1) {
						xhsDao.update(UpdateAid.class, Chain.make("status", 2), Cnd.where("id", "=", info.getId()));
						// org.bson.Document newdoc = new
						// org.bson.Document();
						// newdoc.put("comments", comment);
						// coll.updateOne(Filters.eq("aid", article_id), new
						// org.bson.Document("$set", newdoc));
						return;
					} catch (Exception e2) {
						xhsDao.update(UpdateAid.class, Chain.make("status", 3), Cnd.where("id", "=", info.getId()));
						logger.error("", e2);
					}

				}else {
					xhsDao.update(UpdateAid.class, Chain.make("status", 3), Cnd.where("id", "=", info.getId()));
				}

				logger.info("#######消费完一条文章" + info.getAid() + "队列剩余:" + TQueue.getAQueue().size() + ",IP:"
						+ IPPoolMap.getQueue().size());

			} catch (Exception e) {

				xhsDao.update(UpdateAid.class, Chain.make("status", 3), Cnd.where("id", "=", info.getId()));

				logger.error("解析html异常:url:" + info.getUrl(), e);
			}

		} catch (Exception e) {
			logger.error("", e);

		}

	}

	public static void main(String[] args) throws Exception {
		String html = new HttpBase().get("http://www.xiaohongshu.com/discovery/item/59872886d1d3b919909ace01", "utf-8",
				null, true);

		Document doc = Jsoup.parse(html);
		Elements es = doc.select("script");
		for (Element e : es) {
			String script = e.html();
			if (script.indexOf("__INITIAL_SSR_STATE__") >= 0) {
				String json = script.substring(script.indexOf("{"), script.lastIndexOf("}") + 1);
				System.out.println(json);

				JSONObject obj = JSONObject.parseObject(json);

			}
		}
		// System.out.println(es);
		// JSONArray array = parseService.parseVal(doc, requestId, url);
	}
}
